#include "array.h"

void Array::bigger_size() {
	Array tmp((size_t)ceil(_capacity*_coeff));
	for (size_t i=0; i < _capacity; i++) {
		tmp.push_back(get(i));
	}
	swap(tmp);
}

Array::Array(size_t const capacity) {
	_data = new int[capacity];
	_capacity = capacity;
	_size = 0;
	_coeff = 1.5;
}

Array::~Array() {
	delete [] _data;
}

int Array::get(size_t const index) const {
	if(index < _size) {
		return _data[index];
	} else {
		//throw out_of_bounds
		return NULL;
	}
}

void Array::set(size_t const index, int const value) {
	if(index < _size) {
		_data[index] = value;
	} else {
		//throw out_of_bounds
	}
}

size_t Array::get_size() const {
	return _size;
}

Array::Array(Array& Array2)  {
	_capacity = Array2._capacity;
	_data = new int[_capacity];
	_size = 0;
	for(size_t i = 0; i < Array2._size; ++i) {
		push_back(Array2.get(i));
	}
	_coeff = Array2._coeff;
}

Array& Array::operator =(Array& Array2) {
	Array tmp(Array2);
	swap(tmp);
	return *this;
}

void Array::swap(Array& Array) {
	std::swap(_size, Array._size);
	std::swap(_data, Array._data);
	std::swap(_capacity, Array._capacity);
	std::swap(_coeff, Array._coeff);
}

void Array::push_back(int const value) {
	if(_size >= _capacity) {
		bigger_size();
	}
	set(_size++, value);
}
